<!-- 红包管理 -->
<template>
  <div class="table">
    <div class="container">
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item> <i class="el-icon-search"></i> 查询条件： </el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-row :gutter="10">
        <el-col :xl="6" :lg="6" :md="12" :sm="24" class="mb10">
          <el-input v-model.trim="searchCondition.mobile" placeholder="手机号" clearable></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" class="mb10">
          <el-input v-model.trim="searchCondition.store_name" placeholder="店铺" clearable></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" class="mb10">
          <el-input v-model.trim="searchCondition.contacts_name" placeholder="姓名" clearable></el-input>
        </el-col>
        <el-col :xl="6" :lg="6" :md="12" :sm="24" class="mb10">
          <el-space wrap>
            <el-button type="success" :icon="Search" :disabled="disabledbtn" @click="searchInfo">搜索</el-button>
            <el-button type="warning" :icon="Refresh" :disabled="disabledbtn" @click="resetSearch">重置</el-button>
          </el-space>
        </el-col>
      </el-row>
      <div class="crumbs">
        <el-breadcrumb separator="/">
          <el-breadcrumb-item><i class="el-icon-lx-cascades"></i>查询结果：</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <div class="handle-box">
        <el-button type="primary" :icon="Money" class="mr10" v-permission="'recharge'" :disabled="disabledbtn" @click="handleRecharge">充值</el-button>
      </div>
      <el-table :data="tableList" border class="table" ref="multipleTable" v-loading="loading">
        <el-table-column type="index" align="center"> </el-table-column>
        <el-table-column prop="mobile" label="手机号" align="center"> </el-table-column>
        <el-table-column prop="store_name" label="店铺" align="center">
          <template #default="scope">
            {{ [null, ""].includes(scope.row.store_name) ? "--" : scope.row.store_name }}
          </template>
        </el-table-column>
        <el-table-column prop="contacts_name" label="姓名" align="center">
          <template #default="scope">
            {{ [null, ""].includes(scope.row.contacts_name) ? "--" : scope.row.contacts_name }}
          </template>
        </el-table-column>
        <el-table-column prop="money" label="红包金额" align="center"> </el-table-column>
        <el-table-column label="操作" width="190" align="center">
          <template #default="scope">
            <div class="control-flex">
              <el-button type="primary" link :icon="More" class="orange" @click="handleToRedEnvelopeInfo(scope.row)">明细</el-button>
            </div>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination">
        <el-pagination
          background
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          layout="total, sizes, prev, pager, next, jumper"
          :page-size="searchCondition.size"
          :current-page="searchCondition.current"
          :page-sizes="[10, 20, 30, 40, 50, 100]"
          :total="total"
          :hide-on-single-page="false">
        </el-pagination>
      </div>
    </div>
    <!-- 充值记录弹框 -->
    <RechargeDialog v-model:editVisible="editVisible" v-model:form="rechargeForm" @handleSuccess="handleRechargeSuccess" />
  </div>
</template>

<script setup lang="ts" name="redEnvelopeManagement">
import RechargeDialog from "@/components/moneyManager/redEnvelopeManagement/RechargeDialog.vue";
import { Search, Refresh, More, Money } from "@element-plus/icons-vue";
import { useDictData } from "@/hooks/network/useDictData";
import { useState } from "@/hooks/useState";
import type { IDictData } from "@/types/dict";
import type { IRedPacket, IRedPacketRecharge, ISearchRedPacket } from "@/types/moneyManager/redPacket";
import router from "@/router";
import { getRedEnvelopeManagementInfo } from "@/utils/api/redPacket";
const { disabledbtn, loading, toggleState } = useState();

const total = ref(0);
const editVisible = ref(false);
const searchCondition = reactive<ISearchRedPacket>({
  store_name: "",
  mobile: "",
  contacts_name: "",
  current: 1,
  size: 20
});
const tableList = ref<Array<IRedPacket>>([]);
const masterLogType = ref<Array<IDictData>>([]);
const rechargeForm = reactive<IRedPacketRecharge>({
  mobile: "",
  amount: "",
  remark: ""
});
/* 搜索 */
function searchInfo() {
  searchCondition.current = 1;
  searchCondition.size = 20;
  getTableInfo();
}

/**
 * 重置搜索内容
 */
function resetSearch() {
  searchCondition.store_name = "";
  searchCondition.mobile = "";
  searchCondition.contacts_name = "";
  searchInfo();
}
/* 显示第几页 */
function handleCurrentChange(val: number) {
  searchCondition.current = val;
  getTableInfo();
}

/* 每页显示几条 */
function handleSizeChange(val: number) {
  searchCondition.size = val;
  getTableInfo();
}

/* 请求：获取表格中数据 */
function getTableInfo() {
  toggleState(true);
  getRedEnvelopeManagementInfo<IRedPacket, ISearchRedPacket>(searchCondition)
    .then(res => {
      tableList.value = res.result.data;
      total.value = res.result.total;
    })
    .finally(() => {
      toggleState(false);
    });
}

/* 显示充值金额模态框 */
function handleRecharge() {
  editVisible.value = true;
}
function handleRechargeSuccess() {
  getTableInfo();
}
/* 跳转到红包信息页面 */
function handleToRedEnvelopeInfo(row: IRedPacket) {
  router.push({
    name: "redEnvelopeInfo",
    query: { mobile: row.mobile }
  });
}

let { getDictData } = useDictData();
getDictData("MASTER_LOG_TYPE", masterLogType);
onMounted(() => {
  getTableInfo();
});
</script>

<style lang="scss" scoped>
/* layout-布局 */
.el-row:last-child {
  margin-bottom: 0;
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 36px;
}
.row-bg {
  padding: 10px 0;
  background-color: #f9fafc;
}

.handle-box {
  margin-bottom: 20px;
}

.handle-select {
  width: 120px;
}

.handle-input {
  width: 300px;
  display: inline-block;
}
.del-dialog-cnt {
  font-size: 16px;
  text-align: center;
}
.table {
  width: 100%;
  font-size: 14px;
}
.red {
  color: #ff0000;
}
.mr10 {
  margin-right: 10px;
}
</style>
